package com.example.chang.law_doc.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chang.law_doc.R;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by chang on 2017/12/2.
 * 项目描述：
 */

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_account, et_password, et_pwd_again;
    private Button btn_register;
    TextView tv_title;
    TextView tv_left;
    ImageView iv_left;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        initView();
    }

    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("注册");
        tv_left = (TextView) findViewById(R.id.tv_back_header);
        tv_left.setOnClickListener(this);

        iv_left= (ImageView) findViewById(R.id.iv_left);
        iv_left.setOnClickListener(this);

        et_account = (EditText) findViewById(R.id.et_account);
        et_password = (EditText) findViewById(R.id.et_password);
        et_pwd_again = (EditText) findViewById(R.id.et_pwd_again);
        btn_register = (Button) findViewById(R.id.btn_register);
        btn_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back_header:
            case R.id.iv_left:
                finish();
                break;
            case R.id.btn_register:
                String name = et_account.getText().toString().trim();
                String pass = et_password.getText().toString().trim();
                String passAgain = et_pwd_again.getText().toString().trim();
                if (!TextUtils.isEmpty(name) & !TextUtils.isEmpty(pass) & !TextUtils.isEmpty(passAgain)) {
                    if (pass.equals(passAgain)) {
                        BmobUser user = new BmobUser();
                        user.setUsername(name);
                        user.setPassword(pass);
                        user.signUp(new SaveListener<BmobUser>() {
                            @Override
                            public void done(BmobUser bmobUser, BmobException e) {
                                if (e == null) {
                                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                    finish();
                                } else {
                                    Toast.makeText(RegisterActivity.this, "注册失败" + e.toString(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }
                break;
        }
    }
}
